function get_from_php_session() {
	var data = 'action=get';
	var path = 'http://' + location.hostname + '/systems/campaign/session.php';

	// Get utm_codes from the php session
	$.ajax({
		url: path,
		data: data,
		cache: false,
		async: false,
		success: function(json) {
			var domain = window.location.hostname.replace('www.', '');
			var utm_codes = eval('(' + json + ')');
			var utm_string = '';
			
			// only show the url attribute if it's set
			if(utm_codes['utm_campaign']) {
				// set first character for the query string
				utm_string = '?' + utm_string + 'utm_campaign=' + utm_codes['utm_campaign'];
				
				// when utm_campaign is set, utm_source and utm_medium must be set, else Google Analytics will fail to load
				// fill them with there own value, else with the utm_campaign value
				utm_string += (utm_codes['utm_medium']) ? '&utm_medium=' + utm_codes['utm_medium'] : '&utm_medium=' + utm_codes['utm_campaign'];
				utm_string += (utm_codes['utm_source']) ? '&utm_source=' + utm_codes['utm_source'] : '&utm_source=' + utm_codes['utm_campaign'];
				
				// set values with there own value
				if(utm_codes['utm_content']) utm_string += '&utm_content=' + utm_codes['utm_content'];
				if(utm_codes['utm_term']) utm_string += '&utm_term=' + utm_codes['utm_term'];
				if(utm_codes['utm_term']) utm_string += '&keywords=' + utm_codes['utm_term'];
				if(utm_codes['utm_campaign']) utm_string += '&aid=' + utm_codes['utm_campaign'];
				if(utm_codes['emailmarketing_id']) utm_string += '&emailmarketing_id=' + utm_codes['emailmarketing_id'];
				if(utm_codes['m_i']) utm_string += '&m_i=' + utm_codes['m_i'];
			}
			
			var js_params  = get_js_params();

			switch(js_params['sort']) {
				case 'hypotheek-aanvragen-totaal':
					var sort = '/hypotheek/hypotheek-aanvragen-totaal';
					break;
				case 'hypotheek-aanvragen':
					var sort = '/hypotheek/hypotheek-aanvragen';
					break;
				case 'maximale-hypotheek-berekenen':
					var sort = '/hypotheek/maximale-hypotheek-berekenen';
					break;
				case 'lening-aanvragen':
					var sort = '/lenen/lening-aanvragen';
					break;
				case 'lening-aanvragen-totaal':
					var sort = '/lenen/lening-aanvragen-totaal';
					break;
				case 'afabhypotheek-aanvragen':
					var sort = '/hypotheek/afabhypotheek-aanvragen';
					break;
			}
			
			if(js_params['sub']) {
				var source = 'http://' + js_params['sub'] + '.' + domain + sort + utm_string;
			} else {
				var source = 'http://formulier.' + domain + sort + utm_string;
			}

			if(js_params['advisor']) {
				source += ((utm_string) ? '&' : '?') + 'advisor=' + js_params['advisor'];
			}
			
			//var source = 'http://formulier.' + domain + sort + utm_string;
			var iframe = '<iframe id="formulier" name="formulier" width="{width}" scrolling="no" height="{height}" frameborder="0" src="{source}" marginheight="0" marginwidth="0"></iframe>';
				iframe = iframe.replace('{width}', ((js_params['width']) ? js_params['width'] : ''));
				iframe = iframe.replace('{height}', ((js_params['height']) ? js_params['height'] : ''));
				iframe = iframe.replace('{source}', source);

			// Set domain to the same as the iframe (for security issue)
			document.domain = domain;
			document.write(iframe);
			
			// Call the resize_iframe function when the iframe loads
			$('#formulier').load(function() {
				resize_iframe();
				analyse_step();
			})
		}
	})
}

frame_height = 0; // frame_height saver
step2 = false; // we always begin with step 1

function resize_iframe() {
	try { // catch error on a iframe page switch
		if($('#formulier').contents().find('body').height() != frame_height) {
			$('#formulier').height($('#formulier').contents().find('body').height() + 10);
			frame_height = $('#formulier').contents().find('body').height();
		}
	} catch(e) {}

	setTimeout(resize_iframe, 200);
}

function analyse_step() {
	try {
		var split = document.getElementById('formulier').contentWindow.location.href.split('/');
		
		if((split[4] == 'lening-aanvragen-aanvullende-informatie') && !step2) {
			var path = 'http://' + location.hostname + '/systems/campaign/email_id.php';
			step2 = true;
			
			$.ajax({
				url: path,
				cache: false,
				async: false
			});
		} else if(!step2) {
			setTimeout(analyse_step, 200);
		}
	} catch(e) {}
}

function get_js_params() {
	var query = Array();

	$('script').each(function() {
		if(/iframe.js/.test($(this).attr('src'))) {
			var params = $(this).attr('src').split('?')[1];
			
			if(!params) return false;
			
			params = params.split('&');
			
			for(var i in params) {
				params[i] = params[i].split('=');
				query[params[i][0]] = params[i][1];
			}
			
			return false; // aka break for a jQuery each loop
		}
	})

	return (query) ? query : false;
}

get_from_php_session();